package com.yunzo.coc.handle;

import com.jfinal.core.JFinal;
import com.jfinal.handler.Handler;
import com.jfinal.render.RenderFactory;
import com.yunzo.coc.common.bean.Code;
import com.yunzo.coc.common.bean.BaseResponse;
import com.yunzo.coc.common.utils.DecriptUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;

/**
 * 处理404接口*
 * @author xiaobo
 * @date 15-8-4
 * @package com.yunzo.coc.project
 */
public class APINotFoundHandler extends Handler {
    @Override
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
    	Calendar time = Calendar.getInstance();
        if (!target.startsWith("/api")) {
            this.nextHandler.handle(target, request, response, isHandled);
            return;
        }
        
        if (JFinal.me().getAction(target, new String[1]) == null) {
            isHandled[0] = true;
            try {
                request.setCharacterEncoding("utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            RenderFactory.me().getJsonRender(new BaseResponse(Code.NOT_FOUND, "resource is not found")).setContext(request, response).render();
        } else {
        	  String s1= "A6999598726143";
        	  String key="47B07581-A0CF-3F77-76F7-D6E8600ECBC4";
              String s2= request.getHeader("X-APICloud-AppKey");
              
              System.out.println(s1);
              System.out.println(s2);
              System.out.println("时间："+(time.get(Calendar.MONTH) + 1)+","+time.get(Calendar.DATE)+","+time.get(Calendar.HOUR)+","+time.get(Calendar.MINUTE));
              String appKey = DecriptUtils.SHA1((time.get(Calendar.MONTH) + 1)+s1+time.get(Calendar.DATE)+"YUNZO"+time.get(Calendar.HOUR)+ key+"YUNZO"+time.get(Calendar.MINUTE));
              System.out.println(appKey);
              String [] temp=null;
              if(s2!=null)
            	  temp=s2.split(".");
              if(temp!=null)
              {
            	  if(temp.length>1)
            		  System.out.println(temp[0].equals(appKey));
              }
             
            this.nextHandler.handle(target, request, response, isHandled);
        }
    }
}
